package logic

import (
	"box-fresh-api/models/mysql"
	"context"
	"errors"

	"box-fresh-api/boxrpc/boxrpc"
	"box-fresh-api/boxrpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
	"gorm.io/gorm"
)

type GoodAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGoodAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GoodAddLogic {
	return &GoodAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GoodAddLogic) GoodAdd(in *boxrpc.GoodAddRequest) (*boxrpc.GoodAddResponse, error) {
	// todo: add your logic here and delete this line
	var goods mysql.Goods
	err := goods.FindGoodById(mysql.DB, in.GoodsId)
	if err != nil {
		return nil, errors.New("这件商品已存在")
	}
	goods = mysql.Goods{
		Model: gorm.Model{ID: uint(in.GoodsId)},
		Name:  in.Name,
		Price: float64(in.Price),
		Num:   in.Num,
	}
	err = goods.GoodAdd(mysql.DB)
	if err != nil {
		return nil, err
	}
	return &boxrpc.GoodAddResponse{
		GoodsId: int64(goods.ID),
	}, nil
}
